package com.codegeek.project.communicate;

import com.alibaba.fastjson.JSONObject;
import com.codegeek.project.mybatisTypeHandler.struct.CGSensorTypeParam;
import com.codegeek.project.slope.domain.SlopeMonitorItem;
import com.codegeek.project.slope.domain.SlopeSensor;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wj
 * @ClassName
 * @Description
 * @date 2020-03-24
 */
@Component
public class SlopeSensorHandleService {

    private static final Logger _logger = LoggerFactory.getLogger(SlopeSensorHandleService.class);
    public static final org.apache.logging.log4j.Logger logger = LogManager.getLogger("FileUtils");

    private ExecutorService executorService=Executors.newFixedThreadPool(20);

    @Autowired
    private RedisDBService redisDBService;

    private final static double PIE=3.141592653589793;

    //测斜仪数据处理
    public void pushGradioGraphData(String imei,Float angleX,Float angleY,Float temp,Date time){
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    SlopeSensor slopeSensor = redisDBService.getOrCreateDeviceInfo(imei);
                    if (slopeSensor != null) {
                        Map<String, Float> valueMap = new HashMap<>();
                        List<CGSensorTypeParam> paramList = slopeSensor.getBaseParams();
                        for (CGSensorTypeParam param : paramList) {
                            valueMap.put(param.getName(), param.getValue());
                        }
                        Float h = valueMap.get("H");
                        Float l = valueMap.get("L");
                        Float initX=valueMap.get("initX");
                        Float initY=valueMap.get("initY");
                        float dX= (float) Math.sin(PIE*(double) (angleX-initX)/180)*l*1000;
                        float dY= (float) Math.sin(PIE*(double) (angleY-initY)/180)*l*1000;
                        JSONObject jsData=new JSONObject();
                        jsData.put("X偏移",dX);
                        jsData.put("Y偏移",dY);
                        _logger.info("插入测斜仪缓存数据,X偏移:"+dX+",Y偏移:"+dY);
                        redisDBService.addSensorHistoryData(imei, time, jsData);

                        Map<String,SlopeMonitorItem> itemMap=new HashMap<>();
                        for(SlopeMonitorItem monitorItem:slopeSensor.getMonitorItems()){
                            itemMap.put(monitorItem.getItemName(),monitorItem);
                        }
                        redisDBService.alarmCheck(itemMap.get("X偏移"),slopeSensor.getDevice().getDeviceName(),slopeSensor.getDevice().getNumber(),dX,slopeSensor.getSensorId());
                        redisDBService.handleUpdateMoniterItem(itemMap.get("X偏移").getItemId(),dX);
                        redisDBService.alarmCheck(itemMap.get("Y偏移"),slopeSensor.getDevice().getDeviceName(),slopeSensor.getDevice().getNumber(),dY,slopeSensor.getSensorId());
                        redisDBService.handleUpdateMoniterItem(itemMap.get("Y偏移").getItemId(),dY);
                        redisDBService.updateSensorRealtime(imei,new Date(),jsData);
                    }
                }catch (Exception e){
                    _logger.info("测斜仪数据插入错误，原始数据："+"imei:"+imei+",angleX:"+angleX+",angleY:"+angleY+",Temp:"+temp);
                    e.printStackTrace();
                    logger.error(e);
                    throw new RuntimeException(e);
                }
            }
        });
    }

    //降雨处理
    public void pushRainData(String imei,Float rain,Date time){
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    SlopeSensor slopeSensor = redisDBService.getOrCreateDeviceInfo(imei);
                    if (slopeSensor != null) {
                        JSONObject jsData=new JSONObject();
                        jsData.put("降雨量",rain);
                        _logger.info("插入雨量缓存数据,雨量:"+rain);
                        redisDBService.addSensorHistoryData(imei, time, jsData);

                        Map<String,SlopeMonitorItem> itemMap=new HashMap<>();
                        for(SlopeMonitorItem monitorItem:slopeSensor.getMonitorItems()){
                            itemMap.put(monitorItem.getItemName(),monitorItem);
                        }
                        redisDBService.alarmCheck(itemMap.get("降雨量"),slopeSensor.getDevice().getDeviceName(),slopeSensor.getDevice().getNumber(),rain,slopeSensor.getSensorId());
                        redisDBService.handleUpdateMoniterItem(itemMap.get("降雨量").getItemId(),rain);
                        redisDBService.updateSensorRealtime(imei,new Date(),jsData);
                    }
                }catch (Exception e){
                    _logger.info("雨量数据插入错误，原始数据："+"imei:"+imei+",雨量:"+rain);
                    e.printStackTrace();
                    logger.error(e);
                    throw new RuntimeException(e);
                }
            }
        });
    }


}
